package com.hzjy.manage.service.impl;

import com.hzjy.api.beans.HzjyDelist;
import com.hzjy.api.beans.HzjyProject;
import com.hzjy.api.beans.HzjyProjectAuthor;
import com.hzjy.api.beans.HzjyWorks;
import com.hzjy.api.mapper.ApiProjectMapper;
import com.hzjy.api.mapper.ApiWorksMapper;
import com.hzjy.api.mapper.HzjyProjectAuthorMapper;
import com.hzjy.api.service.impl.ApiDelistServiceImpl;
import com.hzjy.common.enums.TradingStatusEnum;
import com.hzjy.common.utils.SecurityUtils;
import com.hzjy.common.utils.StringUtils;
import com.hzjy.manage.mapper.AuditRecordMapper;
import com.hzjy.manage.mapper.HzjyProductMapper;
import com.hzjy.manage.vo.AuditRecord;
import com.hzjy.system.domain.SysNotice;
import com.hzjy.system.mapper.SysNoticeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.hzjy.manage.vo.HzjyProduct;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 资料审核初审
 *
 * @author SS
 */
@Service
public class AuditDataFirstServiceImpl {
    @Autowired
    private HzjyProductMapper hzjyProductMapper;
    @Autowired
    private AuditRecordMapper auditRecordMapper;
    @Autowired
    private SysNoticeMapper sysNoticeMapper;
    @Autowired
    private ApiProjectMapper apiProjectMapper;
    @Autowired
    private ApiDelistServiceImpl apiDelistService;
    @Autowired
    private ApiWorksMapper apiWorksMapper;
    @Autowired
    private HzjyProjectAuthorMapper hzjyProjectAuthorMapper;


    /**
     * 获取待初审的集合
     *
     * @param hzjyProduct
     * @return
     */
    public List<HzjyProduct> selectlist(HzjyProduct hzjyProduct) {
        return hzjyProductMapper.selectHzjyProductList(hzjyProduct);
    }

    /**
     * 获取详情
     *
     * @param productId
     * @return
     */
    public HzjyProduct selectById(Long productId) {
        return hzjyProductMapper.selectHzjyProductById(productId);
    }

    /**
     * 通过
     *
     * @param auditRecord
     */
    public void agreeAuditData(AuditRecord auditRecord) {
        //插入审核记录
        auditRecord.setCreateTime(new Date());
        auditRecord.setEndState(3); //初审通过状态修改为3
        auditRecord.setType(1);  //
        Long auditId = auditRecordMapper.insertAuditRecord(auditRecord);
        //更新状态
        HzjyProduct hzjyProduct = new HzjyProduct();
        hzjyProduct.setAuditRecordId(auditId);
        hzjyProduct.setProductId(auditRecord.getProductId());
        hzjyProduct.setState(3); //初审通过状态为3
        hzjyProductMapper.updateHzjyProduct(hzjyProduct);
    }

    /**
     * 驳回 驳回信息，驳回人ID，驳回前的状态
     *
     * @param auditRecord
     */
    public void refuseAuditData(AuditRecord auditRecord) {
        //新增审核记录
        auditRecord.setCreateTime(new Date());
        auditRecord.setEndState(1);  //驳回为待修改状态1,
        auditRecord.setType(0); //驳回
        Long auditId = auditRecordMapper.insertAuditRecord(auditRecord);
        //修改记录
        HzjyProduct hzjyProduct = new HzjyProduct();
        hzjyProduct.setAuditRecordId(auditId);
        hzjyProduct.setProductId(auditRecord.getProductId());
        hzjyProduct.setState(1); //驳回状态为1
        //更新作品的状态
        hzjyProductMapper.updateHzjyProduct(hzjyProduct);
    }

    /**
     * 挂牌审核通过，驳回，消息通知
     *
     * @param hzjyProduct
     */
    public void insertTradeAuditNotice(HzjyProduct hzjyProduct) {
        List<AuditRecord> auditRecords = auditRecordMapper.selectAuditRecordList(hzjyProduct.getProductId());
        SysNotice sysNotice = new SysNotice();
        sysNotice.setNoticeType("1");
        sysNotice.setStatus("0");
        sysNotice.setCreateBy(SecurityUtils.getUsername());
        sysNotice.setUserId(hzjyProduct.getUserId().toString());
        if (hzjyProduct.getState() == 3) {
            sysNotice.setNoticeContent("恭喜您挂牌" + hzjyProduct.getProductName() + "初审通过");
            sysNotice.setNoticeTitle("审核通过");
        } else if (hzjyProduct.getState() == 4) {
            sysNotice.setNoticeContent("恭喜您挂牌" + hzjyProduct.getProductName() + "终审通过");
            sysNotice.setNoticeTitle("审核通过");
        } else if (hzjyProduct.getState() == 1) {
            sysNotice.setNoticeContent("您挂牌的" + hzjyProduct.getProductName() + "已被驳回。" + "\r\n" + "驳回原因：" + auditRecords.get(0).getAuditExplain());
            sysNotice.setNoticeTitle("审核驳回");
        }
        sysNoticeMapper.insertNotice(sysNotice);
    }

    /**
     * 查看详情
     *
     * @param productId
     * @return
     */
    public HzjyProduct selectByIdInfo(Long productId) {
        HzjyProduct hzjyProduct = hzjyProductMapper.selectHzjyProductById(productId);
        if (hzjyProduct.getTradingType() == 1) {
            //项目
            HzjyProject queryProject = new HzjyProject();
            queryProject.setProductId(hzjyProduct.getProductId().toString());
            List<HzjyProject> hzjyProjectList = apiProjectMapper.selectHzjyProjectList(queryProject);
            HzjyProject hzjyProject = hzjyProjectList.get(0);
            hzjyProduct.setHzjyProject(hzjyProject);
            //查询对应著作权人列表
            HzjyProjectAuthor hzjyProjectAuthor = new HzjyProjectAuthor();
            hzjyProjectAuthor.setProductId(productId);
            List<HzjyProjectAuthor> authorList = hzjyProjectAuthorMapper.selectHzjyProjectAuthorList(hzjyProjectAuthor);
            hzjyProduct.setAuthorList(authorList);

            //查询作品可出售权利
            if (StringUtils.isNotBlank(hzjyProject.getTradingPower())) {
                //当权力不为null
                String[] salePower = hzjyProject.getTradingPower().split(",");
                List<String> salePowerList = Arrays.asList(salePower);
                List<String> pList = new ArrayList<String>(salePowerList);
                HzjyDelist queryDelist = new HzjyDelist();
                queryDelist.setProductId(hzjyProduct.getProductId());
                //已经摘牌申请的产品
                List<HzjyDelist> haveDelist = apiDelistService.selectHzjyDelistList(queryDelist);

                if (hzjyProduct.getState() == TradingStatusEnum.TradingSuccess.getCode()) {
                    hzjyProduct.setSalability(hzjyProject.getTradingPower());
                } else if (hzjyProduct.getState() == TradingStatusEnum.PartialCompleted.getCode()) {
                    if (hzjyProduct.getTermType() == 1) {
                        if (hzjyProject.getPermissionType() == 0) {
                            //专有许可 只能摘牌一次
                            if (!CollectionUtils.isEmpty(haveDelist)) {
                                //已经有人摘牌当前许可
                                //判断当前摘牌的权利跟
                                for (HzjyDelist delist : haveDelist) {
                                    String[] delistPower = delist.getDelistPower().split(",");
                                    for (String power : delistPower) {
                                        if (salePowerList.contains(power)) {
                                            pList.remove(power);
                                        }
                                    }
                                }
                            }
                            if (CollectionUtils.isEmpty(pList)) {
                                hzjyProduct.setSalability(null);
                            } else {
                                //还有可售出权利
                                String saleP = "";
                                for (String power : pList) {
                                    saleP += power + ",";
                                }
                                saleP = saleP.substring(0, saleP.length() - 1);
                                hzjyProduct.setSalability(saleP);
                            }
                        } else if (hzjyProject.getPermissionType() == 1) {
                            //非专有许可
                            hzjyProduct.setSalability(hzjyProject.getTradingPower());
                        }
                    } else {
                        //转让 权力只能转让一次
                        if (!CollectionUtils.isEmpty(haveDelist)) {
                            //已经有人摘牌当前许可
                            //判断当前摘牌的权利跟
                            for (HzjyDelist delist : haveDelist) {
                                String[] delistPower = delist.getDelistPower().split(",");
                                for (String power : delistPower) {
                                    if (salePowerList.contains(power)) {
                                        pList.remove(power);
                                    }
                                }
                            }
                        }
                        if (CollectionUtils.isEmpty(pList)) {
                            hzjyProduct.setSalability(null);
                        } else {
                            //还有可售出权利
                            String saleP = "";
                            for (String power : pList) {
                                saleP += power + ",";
                            }
                            saleP = saleP.substring(0, saleP.length() - 1);
                            hzjyProduct.setSalability(saleP);
                        }
                    }
                } else if (hzjyProduct.getState() == TradingStatusEnum.PartialCompleted.getCode()) {
                    hzjyProduct.setSalability(null);
                }
            }

            //项目拥有权力数组
            String possessPower = hzjyProject.getPossessPower();
            if (StringUtils.isNotBlank(possessPower)) {
                String[] split = possessPower.split(",");
                hzjyProject.setPossessPowerStr(split);
            }

            //项目标的权力数组
            String salability = hzjyProduct.getSalability();
            if (StringUtils.isNotBlank(salability)) {
                String[] split = salability.split(",");
                hzjyProject.setTradingPowerStr(split);
            }
        } else if (hzjyProduct.getTradingType() == 0) {
            //作品
            HzjyWorks queryWorks = new HzjyWorks();
            queryWorks.setProductId(hzjyProduct.getProductId());
            List<HzjyWorks> hzjyWorksList = apiWorksMapper.selectHzjyWorksList(queryWorks);
            HzjyWorks hzjyWorks = hzjyWorksList.get(0);
            hzjyProduct.setHzjyWorks(hzjyWorks);
            hzjyProduct.setSalability(hzjyWorks.getTradingPower());
        }
        double price = 0;
        price = hzjyProduct.getPrice() * 0.01;
        hzjyProduct.setPrice((int) price);
        return hzjyProduct;
    }
}
